package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.dto.AddResourcePermissionDto;
import com.schoolvisitors.dto.RoleDto;
import com.schoolvisitors.entity.Role;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.RoleService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/admin")
public class RoleController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    // 获取角色列表
    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getRoles")
    public ResultBody getRoles() {
        return ResultBody.success(roleService.getRoles());
    }


    // 添加角色
    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/addRole")
    public ResultBody addRole(@RequestBody @Valid RoleDto roleDto) {
        // 使用 RoleDto 接收更多参数
        Role role = new Role();
        role.setName(roleDto.getName());
        role.setPermissions(roleDto.getPermissions().toString());
        return roleService.addRole(role);
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/updateRole")
    public ResultBody updateRole(@RequestBody @Valid RoleDto roleDto) {
        // 使用 RoleDto 接收更多参数
        Role role = new Role();
        role.setId(roleDto.getId());
        role.setName(roleDto.getName());
        role.setPermissions(roleDto.getPermissions().toString());
        roleService.updateRole(role);
        return ResultBody.success();
    }

    // 删除角色
    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteRole")
    public ResultBody deleteRole(@RequestBody JSONObject requestParams) {
        // 如果是单个参数，使用 JSONObject 来接收
        Long roleId = requestParams.getLong("roleId");
        roleService.deleteRole(roleId);
        return ResultBody.success("角色删除成功");
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getPermissions")
    public ResultBody getPermissions(){
        List<Map<String, String>> data = new ArrayList<>();
        PermissionEnum[] permissionEnums = PermissionEnum.values();
        for (PermissionEnum permissionEnum : permissionEnums) {
            Map<String, String> map = new HashMap<>();
            map.put("desc", permissionEnum.getDesc());
            map.put("value", permissionEnum.name());
            data.add(map);
        }
        return ResultBody.success(data);
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getRoleResources")
    public ResultBody getRoleResources(@RequestParam Long roleId) {
        return ResultBody.success(roleService.getRoleResources(roleId));
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteRoleResource")
    public ResultBody deleteRoleResource(@RequestBody JSONObject params) {
        Long resourceId = params.getLong("resourceId");
        roleService.deleteRoleResource(resourceId);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/addResourcePermission")
    public ResultBody addResourcePermission(@RequestBody AddResourcePermissionDto dto) {

        roleService.addResourcePermission(dto);
        return ResultBody.success();
    }
}
